/*
 *    Copyright 2009 Simone Tripodi
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.memos2;

import java.util.HashSet;
import java.util.Set;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.opensymphony.oscache.base.Config;

/**
 * @author Simone Tripodi (simone.tripodi)
 * @version $Id: MemcachedListenerTestCase.java 94 2009-12-16 09:46:27Z simone.tripodi $
 */
public final class MemcachedListenerTestCase {

    private static final String DEFAULT_GROUP = "listener-group";

    private static final String DEFAULT_KEY = "listener-key";

    private static final Object DEFAULT_STORED_OBJECT = MemcachedListener.class;

    private MemcachedListener listener = new MemcachedListener();

    private Set<String> group = new HashSet<String>();

    @BeforeClass
    public void setUp() {
        this.group.add(DEFAULT_KEY);
    }

    @AfterClass
    public void tearDown() {
        this.listener = null;
        this.group = null;
    }

    @Test(
            dataProvider = "configs",
            dataProviderClass = MemcachedListenerTestCase.class
    )
    public void executeCaching(Config config) throws Exception {
        this.listener.configure(config);

        // 
        assert this.listener.isGroupStored(DEFAULT_GROUP) == false;
        assert this.listener.isStored(DEFAULT_KEY) == false;

        // add object and group, and verify the were stored
        this.listener.store(DEFAULT_KEY, DEFAULT_STORED_OBJECT);
        this.listener.storeGroup(DEFAULT_GROUP, this.group);

        assert this.listener.isGroupStored(DEFAULT_GROUP) == true;
        assert this.listener.isStored(DEFAULT_KEY) == true;

        // remove them from Memcached, and verify they'r no more into it
        this.listener.removeGroup(DEFAULT_GROUP);

        assert this.listener.isGroupStored(DEFAULT_GROUP) == false;
        assert this.listener.isStored(DEFAULT_KEY) == false;
    }

    @DataProvider
    public static Object[][] configs() {
        Config config = new Config();
        config.set("cache.memos2.encryptkeys", "true");
        config.set("cache.memos2.asyncget", "true");
        config.set("cache.memos2.compression", "true");

        return new Object[][] {
                { new Config() },
                { config }
        };
    }

}
